<?php

namespace App\Models\FuYou;

use App\Models\BaseModel;
use App\Models\Traits\KfSoftDeletes;
use App\Models\Traits\KfUid;
use Illuminate\Database\Eloquent\Model;

class WorkOrderFuyouInfoModel extends BaseModel
{
    use KfUid, KfSoftDeletes;

    protected $table = 'work_order_fuyou_info';

    protected $fillable = [
        'id',
        'workOrderNo',
        'fyMchntCd',
        'status',
        'step',
        'shopImg',
        'weChatAuthorizeImg',
        'weChatAuthorizeStatus',
        'alipayAuthorizeImg',
        'alipayAuthorizeStatus',
        'mchKey',
        'orderPrefix',
        'subMchWxId',
        'subMchAlipayId',
        'wxChannelNo',
        'createdByUid',
        'updatedByUid',
        'createdAt',
        'updatedAt',
        'companyName',
        'mchName',
        'mchPassword',
        'secondaryBindingStatus',
        'preSystemApplyStatus',
        'reservedFyTermId',
        'isDeleted',
    ];


    const DEFAULT_STATUS      = 0;//提交到富友
    const DEFAULT_SUC_STATUS  = 1;//成功
    const DEFAULT_FAIL_STATUS = 2;//失败
    const STATUS_MAP          = [
        self::DEFAULT_STATUS      => "审核中",
        self::DEFAULT_SUC_STATUS  => "成功",
        self::DEFAULT_FAIL_STATUS => "失败"
    ];


    //富友状态 0默认待认证 1成功 2失败
    const DEFAULT_SYSTEM_APPLY_STATUS      = 0;//0默认待认证
    const DEFAULT_SYSTEM_APPLY_STATUS_SUC  = 1;//成功
    const DEFAULT_SYSTEM_APPLY_STATUS_FAIL = 2;//失败

    const DEFAULT_FY_TEXT_STATUS = [
        self::DEFAULT_SYSTEM_APPLY_STATUS      => '否',
        self::DEFAULT_SYSTEM_APPLY_STATUS_SUC  => '是',
        self::DEFAULT_SYSTEM_APPLY_STATUS_FAIL => '否',
    ];


    const DEFAULT_STEP_AUDIT                   = 1;//审核中--富友接口资料审核
    const DEFAULT_STEP_BUSINESS_AUTHENTICATION = 2;//商家待认证--富友接口资料审核成功后商家操作
    const DEFAULT_STEP_EMAIL_MCH_NAME_PASSWORD = 3;//系统对接中--接收邮件绑定密钥订单前缀
    const DEFAULT_STEP_SUC                     = 4;//订单完成

    const STEP_MAP = [
        self::DEFAULT_STEP_AUDIT                   => "审核中",
        self::DEFAULT_STEP_BUSINESS_AUTHENTICATION => "商家待认证",
        self::DEFAULT_STEP_EMAIL_MCH_NAME_PASSWORD => "系统对接中",
        self::DEFAULT_STEP_SUC                     => "订单完成",
    ];

    /**
     * 根据id查询详情
     * @param $id
     * @return Model|object|null
     */
    public static function getInfoById($id)
    {
        return self::query()->where(['id' => $id])->orderByDesc('id')->first();
    }

    /**
     * 根据workOrderNo查询详情
     * @param $workOrderNo
     * @return Model|object|null
     */
    public static function getInfoByWorkOrderNo($workOrderNo)
    {
        return self::query()->where(['workOrderNo' => $workOrderNo])->orderByDesc('id')->first();
    }

    /**
     * 新增info
     * @param array $data
     * @return Model
     */
    public static function addInfo(array $data): Model
    {
        return self::query()->create($data);
    }

    /**
     * 修改info
     * @param $id
     * @param $data
     * @return int
     */
    public static function setInfoById($id, $data): int
    {
        return self::query()->where(['id' => $id])->update($data);
    }

    /**
     * 根据companyName查询详情
     * @param $companyName
     * @return Model|object|null
     */
    public static function getInfoByCompanyName($companyName)
    {
        return self::query()->where(['companyName' => $companyName])->orderByDesc('id')->first();
    }

    /**
     * 根据fyMchntCd查询详情
     * @param $fyMchntCd
     * @return Model|object|null
     */
    public static function getInfoByFyMchntCd($fyMchntCd)
    {
        return self::query()->where(['fyMchntCd' => $fyMchntCd])->orderByDesc('id')->first();
    }
}
